if(type_supply_form == 'pesanan_konsumen'){
	$('#tanggal_pesanan').datebox({
		formatter:formatDate,
		value:tanggal_pesanan
	});
}
if(type_supply_form == 'mutasi_generator' || type_supply_form == 'mutasi_promosi'){
	$('#tgl_mutasi').datebox({
		formatter:formatDate,
		value:tanggal_mutasi
	});
}
if(type_supply_form == 'penerimaan_generator' || type_supply_form == 'penerimaan_promosi'){
	$('#tgl_terima_barang').datebox({
		formatter:formatDate,
		value:tgl_terima_barang
	});
	$('#tgl_pengiriman').datebox({
		formatter:formatDate,
		value:tgl_pengiriman
	});
	$('#tgl_penerimaan').datebox({
		formatter:formatDate,
		value:tgl_penerimaan
	});
}

var counter = 0;
if(counter == 0){
	$('#delfield_'+type_supply_form).css({'display':'none'});
}

$("#addfield_"+type_supply_form).on('click',function(){
	counterNext = counter + 1;
		$html = 	"<br />";
		$html +=	'<input type="text" id="dtbarang_'+type_supply_form+'_'+counterNext+'" style="width:200px; border: 1px solid silver; padding: 1px;">';
		$html +=	'	<a id="get_dtbarang_'+type_supply_form+'_'+counterNext+'" onClick="getdatabawah('+counterNext+');" class="l-btn l-btn-plain" href="#">';
		$html +=	'		<span class="l-btn-left">';
		$html +=	'			<span class="l-btn-text icon-search" style="padding-left: 20px;">&nbsp;</span>';
		$html +=	'		</span>';
		$html +=	'	</a>';
		$html +=	'	<input type="hidden" id="id_barang_'+type_supply_form+'_'+counterNext+'" name="id_barang[]">&nbsp;&nbsp;';
		$html +=	"	<input type='hidden' value='addnya' id='' name='parameter[]'>";
		$html +=	"	<input type='hidden' value='' id='' name='id_detail[]'>";
		$html +=	'<input type="text" id="quantity_'+type_supply_form+'_'+counterNext+'" name="quantity[]" style="width:50px; border: 1px solid silver; padding: 1px;">'
		$html +=	'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
		$html +=	'<input type="text" id="satuan_'+type_supply_form+'_'+counterNext+'" name="satuan[]" style="width:50px; border: 1px solid silver; padding: 1px;">'
		$html +=	'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
		$html +=	'<input type="text" id="total_nominal_'+type_supply_form+'_'+counterNext+'" name="total_nominal[]" style="width:50px; border: 1px solid silver; padding: 1px;">'
		$html +=	'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
		
		if(type_supply_form == 'penerimaan_generator' || type_supply_form == 'penerimaan_promosi'){		
			$html +=	'<input type="text" id="qty_terima_'+type_supply_form+'_'+counterNext+'" name="qty_terima[]" style="width:50px; border: 1px solid silver; padding: 1px;">'
			$html +=	'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
			$html +=	'<input type="text" id="selisih_'+type_supply_form+'_'+counterNext+'" name="selisih[]" style="width:50px; border: 1px solid silver; padding: 1px;">'
			$html +=	'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
			$html +=	'<input type="text" id="keterangan_'+type_supply_form+'_'+counterNext+'" name="keterangan[]" style="width:50px; border: 1px solid silver; padding: 1px;">'
		}
		
		$html +=	"<br /><span id='"+type_supply_form+"_add"+counterNext+"' style='margin-left:0px;'></span>";
		$("#"+type_supply_form+"_add"+counter).html($html);
	counter++; 
	$('#delfield_'+type_supply_form).removeAttr('style');
});

$('#delfield_'+type_supply_form).on('click', function(){
	no = counter - 1;
	$("#"+type_supply_form+"_add"+no).html("&nbsp;");
	counter--;
	
	if(counter == 0){
		$('#delfield_'+type_supply_form).css({'display':'none'});
	}
});


$('#get_dealer_'+type_supply_form).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':type_supply_form, 'type_window':'dealer'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});
$('#get_dealer_transfer_'+type_supply_form).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':type_supply_form, 'type_window':'dealer_transfer'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});
$('#get_barang_'+type_supply_form).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':type_supply_form, 'type_window':'barang'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});
$('#get_gudang_'+type_supply_form).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':type_supply_form, 'type_window':'gudang'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});

$('#get_mutasi_'+type_supply_form).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':type_supply_form, 'type_window':'mutasi'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});

$('#get_penerima_'+type_supply_form).on('click', function(){
	$.post('supply/getdisp/searchwindow',{'type':type_supply_form, 'type_window':'penerima'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});
$('#get_pengecek_'+type_supply_form).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':type_supply_form, 'type_window':'pengecek'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});
$('#get_pengirim_'+type_supply_form).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':type_supply_form, 'type_window':'pengirim'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});


$('#btnSend_'+type_supply_form).bind('click', function(){
	submit_form(type_supply_form+'_form', function(resp){
		if(resp == 1){
			kembali_supply(type_supply_form);
			$.messager.alert('Sukses','Data Tersimpan','Sukses');
		}else{
			//$.messager.alert('Gagal','Data Gagal Tersimpan','Gagal');
			$.messager.alert('Gagal',resp,'Gagal');
		}
		
	})
});

function kembali_supply(typenya){
	_post = [{name:'type', value: typenya}];		
	$('#supply_'+typenya).html('');
	$('#supply'+typenya).addClass("loading");
	$.post(host+'supply/gettab/', _post, function(resp) {
		$('#supply'+typenya).removeClass('loading')
		$('#supply_'+typenya).html(resp);						
	});	
}

function getdatabawah(nomernya){
	$.post('home/generaldisplay/searchwindow',{'type':type_supply_form, 'type_window':'barang_bawah', nomer:nomernya},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
}


